<?php
class RedisModel extends BpfModel
{
  public function __init()
  {
   $conf = $this->getIni('redis', []);
  }
  private static $_redis = null;

  private function _connect()
  {
    if (self::$_redis) {
      return;
    }
    $conf = $this->getIni('redis', []);
    $redis = new Redis();
    if (!($redis->pconnect(
      $conf['host'] ?? '127.0.0.1',
      $conf['port'] ?? 6379,
      $conf['timeout'] ?? 0
    ))) {
      throw new Error('Redis connect error');
    }
    if (isset($conf['passwd'])) {
      if (!($redis->auth($conf['passwd']))) {
        throw new Error('Redis connection authenticate fail');
      }
    }
    self::$_redis = $redis;
  }

  function __call($method, $args) {
    if (in_array($method, [ 'connect', 'pconnect', 'close' ])) {
      return;
    }
    if (!self::$_redis) {
      $this->_connect();
    }
    return call_user_func_array([ self::$_redis, $method ], $args);
  }
}
